package com.augurit.wf.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.entity.OmOrg;
import com.augurit.common.system.service.org.IAcRoleService;
import com.augurit.common.system.service.org.IOmOrgService;
import com.augurit.common.system.service.org.IOmPositionService;
import com.augurit.common.system.service.org.IOmUserService;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.common.system.web.org.form.AcRoleForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.util.lang.CollectionUtils;
import com.augurit.wf.common.convertor.SelectUserConvertor;
import com.augurit.wf.common.dao.Jbpm4HistTaskDao;
import com.augurit.wf.common.entity.Jbpm4HistTask;
import com.augurit.wf.common.service.ISelectUserService;
import com.augurit.wf.jbpmex.assignee.AssigneeTreeNode;
import com.augurit.wf.jbpmex.assignee.AssigneeUtils;

@Service
@Transactional
public class SelectUserServiceImpl implements ISelectUserService{

	@Autowired
	private IOmUserService omUserService;
	
	@Autowired
	private IAcRoleService acRoleService;
	
	@Autowired
	private IOmOrgService omOrgService;
	
	@Autowired
	private IOmPositionService omPositionService;
	@Autowired
	private Jbpm4HistTaskDao jbpm4HistTaskDao;
	
	public List<AssigneeTreeNode> getChildrens(AssigneeTreeNode node, String assigneeRange,LoginUserForm userForm,String taskInstDbid){
		List<AssigneeTreeNode> result = null;
		
		if(node != null && assigneeRange != null && assigneeRange.trim().length() > 0){
			// 获取根节点子节点
			if(AssigneeUtils.isRootNode(node)){
				result = AssigneeUtils.getRootNodeChildren(assigneeRange);
				
				for(AssigneeTreeNode childRoot : result){
					// 获取角色根节点子节点
					if(AssigneeUtils.isRoleNode(childRoot)){
						childRoot.setChildren(this.getRoleRootChildren(assigneeRange));
					}
					// 获取用户根节点子节点
					else if(AssigneeUtils.isUserNode(childRoot)){
						childRoot.setChildren(this.getUserRootChildren(assigneeRange));
					}
					// 获取机构根节点子节点
					else if(AssigneeUtils.isOrgNode(childRoot)){
						childRoot.setChildren(this.getOrgRootChildren(assigneeRange));
					}
					// 获取岗位根节点子节点
					else if(AssigneeUtils.isPosNode(childRoot)){
						childRoot.setChildren(this.getPosRootChildren(assigneeRange));
					}
				}
			}
			// 获取角色节点子节点
			else if(AssigneeUtils.isRoleNode(node)){
				
				result = this.getRoleChildrens(node.getRoleId(),assigneeRange,userForm,taskInstDbid);
			}
			// 获取机构节点子节点
			else if(AssigneeUtils.isOrgNode(node)){
				result = this.getOrgChildren(node.getOrgId());
			}
			// 获取岗位节点子节点
			else if(AssigneeUtils.isPosNode(node)){
				result = this.getPosChildren(node.getPosId());
			}
			// 获取工作组节点子节点
			else if(AssigneeUtils.isGroupNode(node)){
				
			}
		}
		
		return result;
	}
	

	public List<AssigneeTreeNode> getChildren(AssigneeTreeNode node, String assigneeRange){
		List<AssigneeTreeNode> result = null;
		
		if(node != null && assigneeRange != null && assigneeRange.trim().length() > 0){
			// 获取根节点子节点
			if(AssigneeUtils.isRootNode(node)){
				result = AssigneeUtils.getRootNodeChildren(assigneeRange);
				
				for(AssigneeTreeNode childRoot : result){
					// 获取角色根节点子节点
					if(AssigneeUtils.isRoleNode(childRoot)){
						childRoot.setChildren(this.getRoleRootChildren(assigneeRange));
					}
					// 获取用户根节点子节点
					else if(AssigneeUtils.isUserNode(childRoot)){
						childRoot.setChildren(this.getUserRootChildren(assigneeRange));
					}
					// 获取机构根节点子节点
					else if(AssigneeUtils.isOrgNode(childRoot)){
						childRoot.setChildren(this.getOrgRootChildren(assigneeRange));
					}
					// 获取岗位根节点子节点
					else if(AssigneeUtils.isPosNode(childRoot)){
						childRoot.setChildren(this.getPosRootChildren(assigneeRange));
					}
				}
			}
			// 获取角色节点子节点
			else if(AssigneeUtils.isRoleNode(node)){
				
				result = this.getRoleChildren(node.getRoleId());
			}
			// 获取机构节点子节点
			else if(AssigneeUtils.isOrgNode(node)){
				result = this.getOrgChildren(node.getOrgId());
			}
			// 获取岗位节点子节点
			else if(AssigneeUtils.isPosNode(node)){
				result = this.getPosChildren(node.getPosId());
			}
			// 获取工作组节点子节点
			else if(AssigneeUtils.isGroupNode(node)){
				
			}
		}
		
		return result;
	}
	
	public List<AssigneeTreeNode> getPosRootChildren(String assigneeRange){
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			List<OmPositionForm> positions = this.getPositionsFromAssigneeRange(assigneeRange);
			return SelectUserConvertor.convertOmPositionFormList(positions);
		}
		return null;
	}
	
	/**
	 * 获取机构根节点的子节点
	 * @param assigneeRange
	 * @return
	 */
	public List<AssigneeTreeNode> getPosChildren(Long posId){
		if(posId != null){

			List<OmPositionForm> childPositions = omPositionService.getChildPositions(posId);
			List<OmUserForm> users = omPositionService.getUsersByPosId(posId);
			
			return SelectUserConvertor.convertToPosChildren(childPositions, users);
		}else
			return null;
	}
	
	/**
	 * 获取机构根节点的子节点
	 * @param assigneeRange
	 * @return
	 */
	public List<AssigneeTreeNode> getOrgRootChildren(String assigneeRange){
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			List<OmOrgForm> orgs = this.getOrgsFromAssigneeRange(assigneeRange);
			return SelectUserConvertor.convertOmOrgFormList(orgs);
		}
		return null;
	}

	
	
	/**
	 * 获取普通机构节点的子节点
	 * @param orgId
	 * @return
	 */
	public List<AssigneeTreeNode> getOrgChildren(Long orgId){
		if(orgId != null){

			List<OmOrgForm> childOrgs = omOrgService.getChildOrgs(orgId);
			List<OmUserForm> users = omOrgService.getUsers(orgId);
			
			return SelectUserConvertor.convertToOrgChildren(childOrgs, users);
		}
		return null;
	}
	
	
    
	/**
	 * 获取角色根节点的子节点
	 * @param orgId
	 * @return
	 */
	public List<AssigneeTreeNode> getRoleRootChildren(String assigneeRange){
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			List<AcRoleForm> roles = this.getRolesFromAssigneeRange(assigneeRange);
			return SelectUserConvertor.convertAcRoleFormList(roles);
		}
		return null;
	}
	
	/**
	 * 获取普通机构节点的子节点
	 * @param orgId
	 * @return
	 */
	public List<AssigneeTreeNode> getRoleChildren(Long roleId){
		if(roleId != null){
			List<OmUserForm> list = acRoleService.getUsersByDirectRole(roleId);
			return SelectUserConvertor.convertOmUserFormList(list);
		}
		return null;
	}
	
	/**
	 * 获取普通机构节点的子节点
	 * @param orgId
	 * @return
	 */
	public List<AssigneeTreeNode> getRoleChildrens(Long roleId,String assigneeRange,
			LoginUserForm userForm,String taskInstDbid){
		if(roleId != null){
			 List<OmUserForm> list =null;
			if(StringUtils.isNotBlank(assigneeRange)){
				int idex=assigneeRange.indexOf(".");
				String rolename="";
				if(assigneeRange.length()>idex && idex>0){
					rolename=assigneeRange.substring(idex+1,assigneeRange.length());
				}
				 //区排放消纳运输经办人和所长
			     if("qupfxn_jbr".equals(rolename) ||"quys_sz".equals(rolename)||
			    		 "qupfxn_sz".equals(rolename) ||"quys_jbr".equals(rolename)){
			    	 OmOrgForm org=userForm.getOrg();
			    	 if(org!=null){
			    		list= acRoleService.getUsersByDirectRoles(roleId,org.getOrgId());
			    	 }
			     }else if("qu_rqc_ck".equals(rolename) ||"qu_rqc_jbr".equals(rolename) || "qu_rqc_cfgld".equals(rolename) ||
			    		 "qu_rqc_cld".equals(rolename) ||"qu_rqc_wfgld".equals(rolename) ||"qu_rqc_wzgld".equals(rolename)){
			    	 OmOrgForm org=userForm.getOrg();
			    	 if(org!=null){
			    		list= acRoleService.getUsersByDirectRoles(roleId,org.getOrgId());
			    	 }
			     }else if("hwk_jbr".equals(rolename) ||"hwk_kz".equals(rolename) || "qjfjz".equals(rolename) ||
			    		 "qjjz".equals(rolename) || "hwk_ck".equals(rolename)){//陆域
			    	 OmOrgForm org=userForm.getOrg();
			    	 if(org!=null){ 
			    		list= acRoleService.getUsersByDirectRoles(roleId,org.getOrgId());
			    		if(list==null){
			    			//如果没有选择人员就查询是哪个窗口收的案
			    			if(StringUtils.isNotBlank(taskInstDbid)){
			    				Jbpm4HistTask task=jbpm4HistTaskDao.get(Long.valueOf(taskInstDbid));
			    				if(task!=null){
			    					String execu=task.getExecution();
			    					String sql=" from Jbpm4HistTask where execution='"+execu+"'";
			    					List<Jbpm4HistTask> tasks=jbpm4HistTaskDao.find(sql);
			    					for(Jbpm4HistTask hist:tasks){
			    						String actname=hist.getActivityName();
			    						String assigne=hist.getAssignee();
			    						if("cksa".equals(actname)){
			    							OmUserForm userform=omUserService.get(assigne);
			    							Long userId=userform.getUserId();
			    							OmOrg omorg=omOrgService.getOrgByUserId(userId);
			    							list= acRoleService.getUsersByDirectRoles(roleId,omorg.getOrgId());
			    							continue;
			    						}
			    					}
			    				}
			    			}
			    			
			    		}
			    		
			    	 }
			     }else if("qu_src_ck".equals(rolename) || "qu_src_jbr".equals(rolename) || 
			    		 "qu_src_kzgld".equals(rolename)  || "qu_src_kfgld".equals(rolename) || 
			    		 "qu_src_jzgld".equals(rolename)  || "qu_src_jfgld".equals(rolename) ||
			    		 "qupfxn_sz".equals(rolename) ||"quys_jbr".equals(rolename)){
			    	 OmOrgForm org=userForm.getOrg();
			    	 if(org!=null){
			    		list= acRoleService.getUsersByDirectRoles(roleId,org.getOrgId());
			    	 }
				}else{
			    	 list = acRoleService.getUsersByDirectRole(roleId);
			     }
			}
			return SelectUserConvertor.convertOmUserFormList(list);
		}
		return null;
	}
	
	
	/**
	 * 获取用户根节点的子节点
	 * @param assigneeRange
	 * @return
	 */
	public List<AssigneeTreeNode> getUserRootChildren(String assigneeRange){
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			List<OmUserForm> users = getUsersFromAssigneeRange(assigneeRange);
			return SelectUserConvertor.convertOmUserFormList(users);
		}
		return null;
	}
	
	
	/**
	 * 获取assignee中特别指定的user
	 * @param assigneeRange
	 * @return
	 */
	public List<OmUserForm> getUsersFromAssigneeRange(String assigneeRange){
		List<String> userKeyList = AssigneeUtils.getUserKeyList(assigneeRange);
		return omUserService.getUsersLoginNames(CollectionUtils.toStringArray(userKeyList));
	}
	
	/**
	 * 获取assigneeRange里的所有角色
	 * @param assigneeRange
	 * @return
	 */
	public List<AcRoleForm> getRolesFromAssigneeRange(String assigneeRange){
		List<String> roleCodeList = AssigneeUtils.getRoleCodeList(assigneeRange);
		return acRoleService.getRolesByCodeList(roleCodeList);
	}
	
	/**
	 * 获取assigneeRange里的所有机构
	 * @param assigneeRange
	 * @return
	 */
	public List<OmOrgForm> getOrgsFromAssigneeRange(String assigneeRange){
		List<String> orgCodeList = AssigneeUtils.getOrgCodeList(assigneeRange);
		return omOrgService.getOrgsByCodeList(orgCodeList);
	}
	
	public List<OmPositionForm> getPositionsFromAssigneeRange(String assigneeRange){
		List<String> posCodeList = AssigneeUtils.getPosCodeList(assigneeRange);
		return omPositionService.getPositionsByCodeList(posCodeList);
	}
	

	
	
	/***************************带keyword的**************/
	public List<AssigneeTreeNode> getChildrenByKeyword(AssigneeTreeNode node, String assigneeRange,
			String keyword,LoginUserForm userForm,String taskInstDbid){
		List<AssigneeTreeNode> result = null;
		
		//查询条件为空
		if(keyword == null || keyword.length()==0)
			return getChildrens(node, assigneeRange,userForm,taskInstDbid);
		
		else{
			if(node != null && assigneeRange != null && assigneeRange.trim().length()>0){
				// 获取根节点子节点
				if(AssigneeUtils.isRootNode(node)){
					
					boolean roleFlag = acRoleService.hasRoleLikeKeyword(this.getRolesFromAssigneeRange(assigneeRange),keyword);
					boolean userFlag = omUserService.hasUserLikeKeyword(this.getUsersFromAssigneeRange(assigneeRange),keyword);
					boolean orgFlag = omOrgService.hasOrgLikeKeyword(this.getOrgsFromAssigneeRange(assigneeRange),keyword);
					boolean posFlag = omPositionService.hasPosLikeKeyword(this.getPositionsFromAssigneeRange(assigneeRange),keyword);
					
					result = AssigneeUtils.getRootNodeChildren(assigneeRange,roleFlag,orgFlag,posFlag,false,userFlag);
					
					for(AssigneeTreeNode childRoot : result){
						// 获取角色根节点子节点
						if(AssigneeUtils.isRoleNode(childRoot)){
							childRoot.setChildren(this.getRoleRootChildren(assigneeRange,keyword));
						}
						// 获取用户根节点子节点
						else if(AssigneeUtils.isUserNode(childRoot)){
							childRoot.setChildren(this.getUserRootChildren(assigneeRange,keyword));
						}
						// 获取机构根节点子节点
						else if(AssigneeUtils.isOrgNode(childRoot)){
							childRoot.setChildren(this.getOrgRootChildren(assigneeRange,keyword));
						}
						// 获取岗位根节点子节点
						else if(AssigneeUtils.isPosNode(childRoot)){
							childRoot.setChildren(this.getPosRootChildren(assigneeRange,keyword));
						}
					}
				}
				// 获取角色节点子节点
				else if(AssigneeUtils.isRoleNode(node)){
					result = this.getRoleChildren(node.getRoleId(),keyword);
				}
				// 获取机构节点子节点
				else if(AssigneeUtils.isOrgNode(node)){
					result = this.getOrgChildren(node.getOrgId(),keyword);
				}
				// 获取岗位节点子节点
				else if(AssigneeUtils.isPosNode(node)){
					result = this.getPosChildren(node.getPosId(),keyword);
				}
				// 获取工作组节点子节点
				else if(AssigneeUtils.isGroupNode(node)){
					
				}
			}
		}
		return result;
	}
	
	public List<AssigneeTreeNode> getAllOrgsByKeyword(AssigneeTreeNode node, String keyword){
		if(node != null && AssigneeUtils.isOrgNode(node) && node.getOrgId() != null){	
			if(keyword != null && keyword.trim().length() > 0){
				if(node.getOrgId().intValue() == -1){
					List<OmOrgForm> orgs = omOrgService.getAll();
					orgs = this.getNeedLoadOrgForms(orgs, keyword);
					return SelectUserConvertor.convertOmOrgFormList(orgs);
				}
				else{
					return this.getOrgChildren(node.getOrgId(), keyword);
				}
			}else{
				if(node.getOrgId().intValue() == -1){
					List<OmOrgForm> orgs = omOrgService.getAll();
					return SelectUserConvertor.convertOmOrgFormList(orgs);
				}
				else{
					return this.getOrgChildren(node.getOrgId());
				}
			}
		}
		return null;
	}
	
	public List<AssigneeTreeNode> getAllRolesByKeyword(AssigneeTreeNode node, String keyword){
		if(node != null && AssigneeUtils.isRoleNode(node) && node.getRoleId() != null){
			if(keyword != null && keyword.trim().length() > 0){
				if(node.getRoleId().intValue() == -1){
					List<AcRoleForm> roles = acRoleService.getAllRoles();
					roles = this.getNeedLoadRoleForms(roles, keyword);
					return SelectUserConvertor.convertAcRoleFormList(roles);
				}
				else{
					return this.getRoleChildren(node.getRoleId(), keyword);
				}
			}else{
				if(node.getRoleId().intValue() == -1){
					List<AcRoleForm> roles = acRoleService.getAllRoles();
					return SelectUserConvertor.convertAcRoleFormList(roles);
				}
				else{
					return this.getRoleChildren(node.getRoleId());
				}
			}
		}
		return null;
	}
	
	public List<AssigneeTreeNode> getAllPositionsByKeyword(AssigneeTreeNode node, String keyword){
		if(node != null && AssigneeUtils.isPosNode(node) && node.getPosId() != null){
			if(keyword != null && keyword.trim().length() > 0){
				if(node.getPosId().intValue() == -1){
					List<OmPositionForm> positions = omPositionService.getAll();
					positions = getNeedLoadPosForms(positions,keyword);
					return SelectUserConvertor.convertOmPositionFormList(positions);
				}
				else{
					return this.getPosChildren(node.getPosId(), keyword);
				}
			}else{
				if(node.getPosId().intValue() == -1){
					List<OmPositionForm> positions = omPositionService.getAll();
					return SelectUserConvertor.convertOmPositionFormList(positions);
				}
				else{
					return this.getPosChildren(node.getPosId());
				}
			}
		}
		return null;
	}
	
	/**
	 * 获取角色根节点的子节点
	 * @param orgId
	 * @return
	 */
	public List<AssigneeTreeNode> getRoleRootChildren(String assigneeRange,String keyword){
		if(assigneeRange != null && assigneeRange.trim().length()>0){
			List<AcRoleForm> roles = this.getRolesFromAssigneeRange(assigneeRange);
			roles = getNeedLoadRoleForms(roles,keyword);
			return SelectUserConvertor.convertAcRoleFormList(roles);
		}
		return null;
	}
	
	/**
	 * 获取普通机构节点的子节点
	 * @param orgId
	 * @return
	 */
	public List<AssigneeTreeNode> getRoleChildren(Long roleId,String keyword){
		if(roleId != null){
			List<OmUserForm> list = acRoleService.getUsersByDirectRole(roleId);
			list = getNeedLoadUserForms(list,keyword);
			return SelectUserConvertor.convertOmUserFormList(list);
		}
		return null;
	}
	
	/**
	 * 获取机构根节点的子节点
	 * @param assigneeRange
	 * @return
	 */
	public List<AssigneeTreeNode> getOrgRootChildren(String assigneeRange, String keyword){
		if(assigneeRange != null && assigneeRange.trim().length()>0 
				&& keyword != null && keyword.trim().length()>0){
			List<OmOrgForm> orgs = this.getOrgsFromAssigneeRange(assigneeRange);
			List<OmOrgForm> result = getNeedLoadOrgForms(orgs,keyword);
			return SelectUserConvertor.convertOmOrgFormList(result);
		}
		return null;
	}
	
	/**
	 * 获取普通机构节点的子节点
	 * @param orgId
	 * @return
	 */
	public List<AssigneeTreeNode> getOrgChildren(Long orgId, String keyword){
		if(orgId != null && keyword != null && keyword.trim().length()>0){

			List<OmOrgForm> childOrgs = omOrgService.getChildOrgs(orgId);
			childOrgs = this.getNeedLoadOrgForms(childOrgs, keyword);
			
			
			List<OmUserForm> users = omOrgService.getUsers(orgId);
			users = this.getNeedLoadUserForms(users, keyword);
			
			return SelectUserConvertor.convertToOrgChildren(childOrgs, users);
		}
		return null;
	}
	
	
	
	/**
	 * 获取用户根节点的子节点
	 * @param assigneeRange
	 * @return
	 */
	public List<AssigneeTreeNode> getUserRootChildren(String assigneeRange,String keyword){
		if(assigneeRange != null && assigneeRange.trim().length()>0 && keyword != null && keyword.trim().length()>0){
			List<OmUserForm> users = getUsersFromAssigneeRange(assigneeRange);
			
			//过滤
			users = this.getNeedLoadUserForms(users, keyword);
			return SelectUserConvertor.convertOmUserFormList(users);
		}
		return null;
	}
	
	public List<AssigneeTreeNode> getPosRootChildren(String assigneeRange,String keyword){
		if(assigneeRange != null && assigneeRange.trim().length()>0 && keyword != null && keyword.trim().length()>0){
			List<OmPositionForm> positions = this.getPositionsFromAssigneeRange(assigneeRange);
			positions = getNeedLoadPosForms(positions,keyword);
			return SelectUserConvertor.convertOmPositionFormList(positions);
		}
		return null;
	}
	
	/**
	 * 获取机构根节点的子节点
	 * @param assigneeRange
	 * @return
	 */
	public List<AssigneeTreeNode> getPosChildren(Long posId,String keyword){
		if(posId != null && keyword != null && keyword.trim().length()>0){

			List<OmPositionForm> childPositions = omPositionService.getChildPositions(posId);
			childPositions = getNeedLoadPosForms(childPositions,keyword);
			List<OmUserForm> users = omPositionService.getUsersByPosId(posId);
			users = getNeedLoadUserForms(users,keyword);
			
			return SelectUserConvertor.convertToPosChildren(childPositions, users);
		}else
			return null;
	}
	
	
	//获取机构列表中的需要加载的机构form
    public List<AcRoleForm> getNeedLoadRoleForms(List<AcRoleForm> list,String keyword){
    	List<AcRoleForm> result = null;
    	if(list != null && list.size()>0&& keyword != null && keyword.trim().length()>0){
    		result = new ArrayList<AcRoleForm>();
    		
			for(AcRoleForm form:list){
				if(this.acRoleService.isRoleLikeKeyword(form.getRoleId(), keyword))
					result.add(form);
			}
    	}
    	return result;
    }
    
	//获取机构列表中的需要加载的机构form
    public List<OmOrgForm> getNeedLoadOrgForms(List<OmOrgForm> list,String keyword){
    	List<OmOrgForm> result = null;
    	if(list != null && list.size()>0&& keyword != null && keyword.trim().length()>0){
    		result = new ArrayList<OmOrgForm>();
    		
			for(OmOrgForm form:list){
				if(this.omOrgService.isOrgLikeKeyword(form.getOrgId(), keyword))
					result.add(form);
			}
    	}
    	return result;
    }
	
    //获取机构列表中的需要加载的机构form
    public List<OmPositionForm> getNeedLoadPosForms(List<OmPositionForm> list,String keyword){
    	List<OmPositionForm> result = null;
    	if(list != null && list.size()>0&& keyword != null && keyword.trim().length()>0){
    		result = new ArrayList<OmPositionForm>();
    		
			for(OmPositionForm form:list){
				if(this.omPositionService.isPosLikeKeyword(form.getPosId(), keyword))
					result.add(form);
			}
    	}
    	return result;
    }
    
  //获取用户列表中的需要加载的用户form
    public List<OmUserForm> getNeedLoadUserForms(List<OmUserForm> list,String keyword){
    	List<OmUserForm> result = null;
    	if(list != null && list.size()>0&& keyword != null && keyword.trim().length()>0){
    		result = new ArrayList<OmUserForm>();
    		
			for(OmUserForm form:list){
				if(this.omUserService.isUserLikeKeyword(form.getUserId(), keyword))
					result.add(form);
			}
    	}
    	return result;
    }
}
